<script setup lang="ts">
import { request } from '@/api'
import router from '@/router'
import { closeToast, showLoadingToast, showToast } from 'vant'

onMounted(() => {
  getAddressList()
})

const addressList = ref<AddressItem[]>([])
// 获得地址列表
const getAddressList = async () => {
  showLoadingToast({ duration: 0, message: '加载中...', forbidClick: true })
  const res = await request.get<MkResponse<AddressItem[]>>('member/address')
  addressList.value = res.data.result
  closeToast()
}

// 设置默认地址
const setAddressDefault = async (item: AddressItem) => {
  await request.put(`member/address/${item.id}`, { isDefault: 0 })
  // 先把所有地址置为非默认
  addressList.value.forEach(item => (item.isDefault = 1))
  // 把当前该地址置为默认
  item.isDefault = 0
  showToast('更新地址成功')
}

// 删除地址
const deleteAddress = async (item: AddressItem) => {
  // 更新网络
  await request.delete<MkResponse<{ id: string }>>(`/member/address/${item.id}`)
  // 更新本地
  addressList.value = addressList.value.filter(addr => addr.id != item.id)
  showToast('删除成功')
}

const refreshing = ref(false)
const onRefresh = async () => {
  refreshing.value = true
  // 重新获取地址列表
  await getAddressList()
  refreshing.value = false
  showToast('更新成功')
  // 手机震动
  mk.vibrator()
}
</script>

<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <div class="address-manager-page">
      <van-swipe-cell v-for="item in addressList" :key="item.id">
        <div class="item">
          <div class="item-wrapper">
            <div class="item-area">
              <span class="item-tag" v-if="item.isDefault == 0">默认</span>
              {{ item.fullLocation }}
            </div>
            <div class="item-address">
              {{ item.address }}
            </div>
            <div class="item-connect">{{ item.receiver }} {{ item.contact }}</div>
          </div>
          <div class="item-edit">
            <!-- 点击去编辑页 传递参数 就显示编辑 -->
            <van-icon
              @click="() => router.push(`/address/edit/${item.id}`)"
              name="edit"
              size="20"
              color="var(--mk-gray)"
            ></van-icon>
          </div>
        </div>
        <template #right>
          <van-button
            square
            @click="() => setAddressDefault(item)"
            type="primary"
            style="height: 100%"
          >
            设为<br />默认
          </van-button>
          <van-button
            @click="() => deleteAddress(item)"
            square
            type="danger"
            style="height: 100%"
            text="删除"
          />
        </template>
      </van-swipe-cell>
  
      <div class="submit"> 
        <!-- 跳转到新建地址页 -->
        <van-button round block type="primary" to="/address/edit">
          新建地址
        </van-button>
      </div>
    </div>
  
  </van-pull-refresh>
</template>

<style lang="css" scoped>
.address-manager-page {
  padding: 8px;
  width: 100%;
  min-height: 100vh;
  position: relative;
  padding-bottom: 80px;
}

.submit {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 16px;
  height: 80px;
}

.item {
  min-height: 108px;
  width: 100%;
  background-color: var(--mk-white);
  border-radius: 2px;
  display: flex;
  margin-bottom: 8px;
}

.item-wrapper {
  padding: 16px;
  flex: 1;
}

.item-area {
  font-size: 14px;
  color: var(--mk-text);
  margin-bottom: 5px;
}

.item-tag {
  display: inline-block;
  border-radius: 4px;
  width: 40px;
  height: 20px;
  line-height: 20px;
  font-size: 12px;
  color: var(--mk-white);
  text-align: center;
  background-image: linear-gradient(
    90deg,
    var(--mk-linear_begin),
    var(--mk-linear_end)
  );
  margin-right: 4px;
}

.item-address {
  font-size: 16px;
  color: var(--mk-black);
  font-weight: 500;
  margin-bottom: 5px;
}

.item-connect {
  font-size: 12px;
  color: var(--mk-text);
}

.item-edit {
  width: 48px;
  display: flex;
  align-items: center;
}

.btn-group {
  height: 100%;
  width: 100px;
  display: flex;
}
</style>
